package com.zhongtf.demo;

import com.alibaba.fastjson.JSON;
import com.zhongtf.entity.AdressEthToken;

import java.util.HashMap;
import java.util.Map;
import static com.zhongtf.utils.BipUtils.getEthAddress;
import static com.zhongtf.utils.RSAUtil.sign;

//企业发票，或者交易所发票
public class client {
    public static void main(String[] args) {
        /**
         * 1.交易所给企业的每个票公钥xpub,票根为m/44'/60'/1'/i，i表示每一张票
         *  2.派生路径为m/44'/60'/1'/0对应的xpub公钥Base58字符串，xpub:xpub6EwLt1EDyThtuQV58UroAigCzRi8iwMRTSSs2peNiSwr2qTR1ffXEPYiGhMJ3pwSx8RKfaABjGhsu9raX27shVVzeDkZSRiz3JFhPCm6Czf
         */
         String pubKeyExtendedBase58="xpub6EwLt1EDyThtuQV58UroAigCzRi8iwMRTSSs2peNiSwr2qTR1ffXEPYiGhMJ3pwSx8RKfaABjGhsu9raX27shVVzeDkZSRiz3JFhPCm6Czf";

        /**
         * 1.企业根据派生路径为m/44'/60'/1'/i 的票公钥xpub生成对应的票号，派生路径m/44'/60'/1'/i/j中，i表示对应的票，j对应相应的票号，获取票号的派生路径m/44'/60'/1'/i/j对应的以太坊地址
         * 2.tokenMap用来存储票号对应的以太坊地址
         */
        Map<Integer, Object> tokenMap = new HashMap<Integer, Object>();
        //生成多张票的ETH地址，假设企业编号为0，票据的序号从1开始，生成1到10的票号
        int   i = 0;
        int   j = 1;
        String token;
        //每张票的签名
        String addressSign;
        //企业私钥,企业自己生成，同时生成后需要将公钥保存到交易所的企业后台
        String privateKeyStr="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIgh0XERN5H8vTpiTmx/UX9DWEL9\n" +
                "QOCM54Vao31gDK1aMT05or5HQC1jQZgNKJvpUEqX/lMXifloVLh1AwtWgGqWSIaLcxO3aLJcK+9b\n" +
                "+QX+vEwP5AhTYvxBhbtY0khgcrft6wq8XzcrQaUkFYbJygHa7eoHcRNMCzu0C/ejIa3zAgMBAAEC\n" +
                "gYBvA0UM84Fhp72YkvppOBp5sa2UU3pIpB9ZfGS+pUkxRM/Koem5SdloKf0fxftLyaQUcIaRtCxs\n" +
                "G0ybb7Cf2la1Yn3ozygW/yewKYw20jZ1AQPLkM9xmYm0ftuqPhP6z0K/N4RdVxCVvT7/bwBDwN0B\n" +
                "RcTIpUvy/RmbVTKhRGD4wQJBANGPsKHK1gSH+laOb6Oy3e0rxTRsV/3rjbISvWld+ZkW6xiSqQW4\n" +
                "uxFJFyyV7OGbT+XKSxYE6d7kCM3b3CtDZ7kCQQCmTIauFWzHcWuV93lATj7yzvyEdzLAv4DCh5V8\n" +
                "CqJ+VNotXpD8Ch069hlKXjETbGZqJBGz9B8c9vOF1z/buIELAkEAzXVxmc8CccLXoBFaR0bQ6v0l\n" +
                "BbbLtnpBnDtfVXT7iCq+KINyD9gEIHhmriH382GQvhzN7TK4EP3F2/8ZJ5mYkQJAKjs23JCeTkvU\n" +
                "WYI+LYFxZPIBmJY4slAvn7cU1CpTN6fOvETOpuLep3GF3Kfs6JTXsU61nDB7kjx4fV27Xs4NHwJB\n" +
                "AIHReSGm7wnJvPo3Cyil5Dkaa4xaZC1t7AnwNDc/SrQTColB/lxJfsIkr2c10STS8fogph55GrF6\n" +
                "zEI6o+1E8W8=";
        while (j < 11) {
            System.out.println("企业票"+i+"的票号的派生路径：m/44'/60'/1'/"+i+"/" + j);
            System.out.println("sequence:" + j);
            token= getEthAddress(j,pubKeyExtendedBase58);
            System.out.println("token:" + token);
            addressSign =sign(token, privateKeyStr);
            System.out.println("addressSign:" + addressSign);
            AdressEthToken adressEthToken=new AdressEthToken();
            String jString=String.valueOf(j);
            adressEthToken.setToken(token);
            //对每个票的以太坊地址用企业私钥进行签名
            adressEthToken.setSign(addressSign);
            tokenMap.put(j, adressEthToken);
            j++;
        }
        //企业端的私钥对数据进行签名,appid+时间戳,appId从企业端管理后台获取
        String appid="1160fc300abce69590556fbb0acec8ca";
        Long timestamp= System.currentTimeMillis()/1000;
        String timestampStr= timestamp.toString();
        String signMessage= appid+timestampStr;
        //获取企业传送数据的企业签名
        String signature= sign(signMessage, privateKeyStr);
        System.out.println("signature:" + signature);

        //发送给交易所票据请求数据
        Map<String, Object> params = new HashMap<String, Object>();
        //票据token 转json
        String tokenMapJson=JSON.toJSONString(tokenMap);
        System.out.println("data:" + tokenMapJson);

        params.put("data",tokenMapJson);
        params.put("time_stamp",timestampStr);
        System.out.println("timestampStr:" + timestampStr);
        params.put("appid",appid);
        params.put("signature",signature);
        System.out.println("signature:" + signature);

        String map =JSON.toJSONString(params);
        System.out.println("map:" + map);
    }
}
//        企业票0的票号的派生路径：m/44'/60'/1'/0/1
//        sequence:1
//        address:0xAc85f833BfAC840eF2AAfD6006c5D51d8CF4848E
//        token:0xAc85f833BfAC840eF2AAfD6006c5D51d8CF4848E
//        addressSign:COfg5ZrikCASyCYfabu7wmHhIevNTpIThluWW+Fmy0aq7bzkoXQym/nrHjtKW3FHljzx2UDHhLulIgoI4uXblIKAD7Pwx0iT4NXM0MBkZRHScDxg5n5Z0O5+alBS7G7zqNDvdQGtP7u86JsojxzRzPeyaj1HSbcTh6qXkwjBxOQ=
//        企业票0的票号的派生路径：m/44'/60'/1'/0/2
//        sequence:2
//        address:0x6A72771cCF1ce5eCd8e10C2a66EC5f7607dAcC1b
//        token:0x6A72771cCF1ce5eCd8e10C2a66EC5f7607dAcC1b
//        addressSign:aaNZt09ChlcggwHYl+omASBsFyjjuPQ3nEuarmMYzR6zFJTKHi5wNLA6Iy/LRxjU77z262kVrGke91ey0ej095HQsg8kKZ4es0puRmHSYbzLazVJKxfuauoMx9MIM44xG5kV79xYNnOUq6LCg3Qg03NUaEET2AZ72YENPeXGABI=
//        企业票0的票号的派生路径：m/44'/60'/1'/0/3
//        sequence:3
//        address:0x0d2539E0fc299965613cD52A8fBe5f1137a01BeD
//        token:0x0d2539E0fc299965613cD52A8fBe5f1137a01BeD
//        addressSign:Jk2IE7RQsJhEQvfJySker30lhlvhDtMZdu0Fdm4BMIKjulcS9Qc10wVIodfHjuwFCKS9mN3VM4xRdT4k5X9Dz29IRpuEP8IpSHkA7ZDaxZHiXolHc566agbbOTKH1vHFOu6dymzf6z8b2l9y4dGQhUc+oqJJ0W1Rfb+NoHy3AxI=
//        企业票0的票号的派生路径：m/44'/60'/1'/0/4
//        sequence:4
//        address:0x6982eCD13f80034823Ed7FBb7139b70D2c7eD737
//        token:0x6982eCD13f80034823Ed7FBb7139b70D2c7eD737
//        addressSign:OGjduE8XnhZzSBGdVF6BvyGKqq4TDa7L7WBeYdQWh2c+o5Smj9B5XGfHFFLgsolLvZa6bJzfdGuu5ds1db5dI3E3DK5CubIv7ivYJwDpMOodjk61m/UWMs2z2LqRoHVFQp5B87wDH7xJkSIveVwMH5c+i/ra+vS/+3S+X89+Gwo=
//        企业票0的票号的派生路径：m/44'/60'/1'/0/5
//        sequence:5
//        address:0xBAF5238bbd4A6083345Bb9348f5d22af05eCeb68
//        token:0xBAF5238bbd4A6083345Bb9348f5d22af05eCeb68
//        addressSign:KoPkMKyOYO/lnoqPV7IGDqYAAyMSoDt3O6w+qMDb1UkudipoBgiq/hdoXvSb/KdRowGftTDFx+FIhDa/KdmISFrcJCYb412KecFW+RBkJmtpageEdvWlL00dEN5M/9ppsmqaZWENl/eokpz0G4BdaDeuSNdVJ2wvF794G4YoGcg=
//        企业票0的票号的派生路径：m/44'/60'/1'/0/6
//        sequence:6
//        address:0xEA60932fdEc4d31D9C244e881514F189A374A2D4
//        token:0xEA60932fdEc4d31D9C244e881514F189A374A2D4
//        addressSign:E5YVGBU5ZwyDQfv7Q7td2ONi9N4m+QhsALEyh5G4EtlCgdYC3lJckxtNeSBg8tvwJRcfX4RZn0lrSbIHBl/OUd1Xw+d6Xnb3PAgQ8gsMfWlQBtrAWnX7PBLrKYioUxX5QBjsgsOKcADQ+aOmea2nIGXzWzOJ3/kOy6s45nnD2YQ=
//        企业票0的票号的派生路径：m/44'/60'/1'/0/7
//        sequence:7
//        address:0x47AEc4596643f071a4538150D78d680425693ef4
//        token:0x47AEc4596643f071a4538150D78d680425693ef4
//        addressSign:g8Pfn4ENDxXPD1tQlPOofZZSKi8P7dYQsteSP1/GAK1x36LetUe3x0D4yU11wm3W4cpG/8wIkKFoaD6/zFVgHZYy1bnLm4FnnQG9vTx5MFDgnE1VdNbkAsuKk2lou4dL8LG6UjUiSeRgPGPdHPbhzBGExX3Ns+bzfXXjvkFvlHU=
//        企业票0的票号的派生路径：m/44'/60'/1'/0/8
//        sequence:8
//        address:0xE6d3EB60b7d190EABD0119c11dE66FD27bc642Ee
//        token:0xE6d3EB60b7d190EABD0119c11dE66FD27bc642Ee
//        addressSign:h3o8/2r0OP8kJIjFqMnxh/LCfEWCOW0K2fX/xlXLubs1C7UfkkGHOwpyvM+cMIU3Zer2e6w1Thd2VtmsxwzaxWe3Ww7fN+V8fxx5psF5RkIudEixVumnB2KJwYtXZpYwQUrs79ouAp9iz9aoFMk2qtNsxIBV0MLXKBeggOYQI9E=
//        企业票0的票号的派生路径：m/44'/60'/1'/0/9
//        sequence:9
//        address:0x951E0D9bA7AAA7B6207fee35Aa2Cf6402aD054F6
//        token:0x951E0D9bA7AAA7B6207fee35Aa2Cf6402aD054F6
//        addressSign:ddR8FWk2pg7oxyjWeI1S/py9v54/N2NsvSE8J72EmUm0fheufL5eat+0CQz3xmuIK9Zq/4dHL5AmzteiOuDzinLBBJWncIWKj9LlhJOSSVvc3ANjBoiIBYNgXj/T/Wc7kkAlUVAF+vt3yv5DOZTo/0aDtdB3d2ajvIcr67GcIPw=
